package com.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.man.BookCtrl;
import com.man.TypeCtrl;
import com.std.Book;
import com.std.Type;

public class UserBookSelectAction  implements SessionAware {
	public static final int SIZE=6;
	private int 	typeId;
	private String 	keyword;
	private int		cnt;
	private int 	curPos;
	private int 	nextPos;
	private Map		sessionMap;	
	private List<Book> bookList;
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		sessionMap=session;
	}
	public int getTypeId() {
		return typeId;
	}
	public void setTypeId(int typeId) {
		this.typeId = typeId;
	}
	public String getKeyword() {
		return keyword;
	}
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	public int getCnt() {
		return cnt;
	}
	public void setCnt(int cnt) {
		this.cnt = cnt;
	}
	public int getCurPos() {
		return curPos;
	}
	public void setCurPos(int curPos) {
		this.curPos = curPos;
	}
	public int getNextPos() {
		return nextPos;
	}
	public void setNextPos(int nextPos) {
		this.nextPos = nextPos;
	}
	public List<Book> getBookList() {
		return bookList;
	}
	public void setBookList(List<Book> bookList) {
		this.bookList = bookList;
	}
	public String execute(){
		System.out.println("UserBookSelectAction.execute()");
		if(curPos==0){
			if(sessionMap.get("curPos")!=null) 
				curPos=Integer.parseInt(sessionMap.get("curPos").toString());
			else curPos=1;
			if(sessionMap.get("typeId")==null) typeId=0;
			else typeId=Integer.parseInt(sessionMap.get("typeId").toString());
		}
		if(keyword==null||keyword==""){
			if(sessionMap.get("keyword")==null) keyword="";
			else keyword=sessionMap.get("keyword").toString();
		}
		BookCtrl bookCtrl=new BookCtrl();
		cnt=bookCtrl.getCount(typeId, keyword);
		if(cnt<curPos){
			curPos=cnt-cnt%SIZE+1;
			if(cnt%SIZE==0) curPos-=SIZE;
		}
		if(curPos<=0) curPos=1;
		bookList=bookCtrl.getItems(typeId, keyword, curPos, curPos+SIZE);
		bookCtrl.close();
		nextPos=curPos+SIZE;
		if(nextPos>cnt) nextPos=curPos;
		sessionMap.put("curPos", curPos);
		sessionMap.put("typeId", typeId);
		sessionMap.put("keyword", keyword);	
		return "success";
	}
}
